home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 242_01 / a63util.c < prev    next >
Text File  |  1989-01-11  |  16KB  |  564 lines

  1. /*
  2.     HEADER:        CUG242;
  3.     TITLE:        63701 Cross-Assembler (Portable);
  4.     FILENAME:    A63UTIL.C;
  5.     VERSION:    0.1;
  6.     DATE:        08/27/1988;
  7.     SEE-ALSO:    A63.H;
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.               63701 Cross-Assembler in Portable C
  13.  
  14.         Copyright (c) 1985,1987 William C. Colley, III
  15.  
  16. Revision History:
  17.  
  18. Ver    Date        Description
  19.  
  20. 0.0    AUG 1987    Derived from version 3.4 of my portable 6800/6801
  21.             cross-assembler.  WCC3.
  22.  
  23. 0.1    AUG 1988    Fixed a bug in the command line parser that puts it
  24.             into a VERY long loop if the user types a command line
  25.             like "A63 FILE.ASM -L".  WCC3 per Alex Cameron.
  26.  
  27. This module contains the following utility packages:
  28.  
  29.     1)  symbol table building and searching
  30.  
  31.     2)  opcode and operator table searching
  32.  
  33.     3)  listing file output
  34.  
  35.     4)  hex file output
  36.  
  37.     5)  error flagging
  38. */
  39.  
  40. /*  Get global goodies:  */
  41.  
  42. #include "a63.h"
  43.  
  44. /*  Make sure that MSDOS compilers using the large memory model know    */
  45. /*  that calloc() returns pointer to char as an MSDOS far pointer is    */
  46. /*  NOT compatible with the int type as is usually the case.        */
  47.  
  48. char *calloc();
  49.  
  50. /*  Get access to global mailboxes defined in A63.C:            */
  51.  
  52. extern char errcode, line[], title[];
  53. extern int eject, listhex;
  54. extern unsigned address, bytes, errors, listleft, obj[], pagelen;
  55.  
  56. /*  The symbol table is a binary tree of variable-length blocks drawn    */
  57. /*  from the heap with the calloc() function.  The root pointer lives    */
  58. /*  here:                                */
  59.  
  60. static SYMBOL *sroot = NULL;
  61.  
  62. /*  Add new symbol to symbol table.  Returns pointer to symbol even if    */
  63. /*  the symbol already exists.  If there's not enough memory to store    */
  64. /*  the new symbol, a fatal error occurs.                */
  65.  
  66. SYMBOL *new_symbol(nam)
  67. char *nam;
  68. {
  69.     SCRATCH int i;
  70.     SCRATCH SYMBOL **p, *q;
  71.     void fatal_error();
  72.  
  73.     for (p = &sroot; (q = *p) && (i = strcmp(nam,q -> sname)); )
  74.     p = i < 0 ? &(q -> left) : &(q -> right);
  75.     if (!q) {
  76.     if (!(*p = q = (SYMBOL *)calloc(1,sizeof(SYMBOL) + strlen(nam))))
  77.         fatal_error(SYMBOLS);
  78.     strcpy(q -> sname,nam);
  79.     }
  80.     return q;
  81. }
  82.  
  83. /*  Look up symbol in symbol table.  Returns pointer to symbol or NULL    */
  84. /*  if symbol not found.                        */
  85.  
  86. SYMBOL *find_symbol(nam)
  87. char *nam;
  88. {
  89.     SCRATCH int i;
  90.     SCRATCH SYMBOL *p;
  91.  
  92.     for (p = sroot; p && (i = strcmp(nam,p -> sname));
  93.     p = i < 0 ? p -> left : p -> right);
  94.     return p;
  95. }
  96.  
  97. /*  Opcode table search routine.  This routine pats down the opcode    */
  98. /*  table for a given opcode and returns either a pointer to it or    */
  99. /*  NULL if the opcode doesn't exist.                    */
  100.  
  101. OPCODE *find_code(nam)
  102. char *nam;
  103. {
  104.     OPCODE *bsearch();
  105.  
  106.     static OPCODE opctbl[] = {
  107.     { NULL,                    0x1b,    "ABA"    },
  108.     { NULL,                    0x3a,    "ABX"    },
  109.     { DIROK + INDEX + IMM8 + REGREQ,    0x89,    "ADC"    },
  110.     { DIROK + INDEX + IMM8,            0x89,    "ADCA"    },
  111.     { DIROK + INDEX + IMM8,            0xc9,    "ADCB"    },
  112.     { DIROK + INDEX + IMM8 + REGREQ,    0x8b,    "ADD"    },
  113.     { DIROK + INDEX + IMM8,            0x8b,    "ADDA"    },
  114.     { DIROK + INDEX + IMM8,            0xcb,    "ADDB"    },
  115.     { DIROK + INDEX + IMM16,        0xc3,    "ADDD"    },
  116.     { TWO_OP + INDEX,            0x71,    "AIM"    },
  117.     { DIROK + INDEX + IMM8 + REGREQ,    0x84,    "AND"    },
  118.     { DIROK + INDEX + IMM8,            0x84,    "ANDA"    },
  119.     { DIROK + INDEX + IMM8,            0xc4,    "ANDB"    },
  120.     { INDEX + REGOPT,            0x48,    "ASL"    },
  121.     { NULL,                    0x48,    "ASLA"    },
  122.     { NULL,                    0x58,    "ASLB"    },
  123.     { NULL,                    0x05,    "ASLD"    },
  124.     { INDEX + REGOPT,            0x47,    "ASR"    },
  125.     { NULL,                    0x47,    "ASRA"    },
  126.     { NULL,                    0x57,    "ASRB"    },
  127.     { REL,                    0x24,    "BCC"    },
  128.     { TWO_OP + BIT_OP + INDEX,        0x71,    "BCLR"    },
  129.     { REL,                    0x25,    "BCS"    },
  130.     { REL,                    0x27,    "BEQ"    },
  131.     { REL,                    0x2c,    "BGE"    },
  132.     { REL,                    0x2e,    "BGT"    },
  133.     { REL,                    0x22,    "BHI"    },
  134.     { REL,                    0x24,    "BHS"    },
  135.     { DIROK + INDEX + IMM8 + REGREQ,    0x85,    "BIT"    },
  136.     { DIROK + INDEX + IMM8,            0x85,    "BITA"    },
  137.     { DIROK + INDEX + IMM8,            0xc5,    "BITB"    },
  138.     { REL,                    0x2f,    "BLE"    },
  139.     { REL,                    0x25,    "BLO"    },
  140.     { REL,                    0x23,    "BLS"    },
  141.     { REL,                    0x2d,    "BLT"    },
  142.     { REL,                    0x2b,    "BMI"    },
  143.     { REL,                    0x26,    "BNE"    },
  144.     { REL,                    0x2A,    "BPL"    },
  145.     { REL,                    0x20,    "BRA"    },
  146.     { REL,                    0x21,    "BRN"    },
  147.     { TWO_OP + BIT_OP + INDEX,        0x72,    "BSET"    },
  148.     { REL,                    0x8d,    "BSR"    },
  149.     { TWO_OP + BIT_OP + INDEX,        0x75,    "BTGL"    },
  150.     { TWO_OP + BIT_OP + INDEX,        0x7b,    "BTST"    },
  151.     { REL,                    0x28,    "BVC"    },
  152.     { REL,                    0x29,    "BVS"    },
  153.     { NULL,                    0x11,    "CBA"    },
  154.     { NULL,                    0x0c,    "CLC"    },
  155.     { NULL,                    0x0e,    "CLI"    },
  156.     { INDEX + REGOPT,            0x4f,    "CLR"    },
  157.     { NULL,                    0x4f,    "CLRA"    },
  158.     { NULL,                    0x5f,    "CLRB"    },
  159.     { NULL,                    0x0a,    "CLV"    },
  160.     { DIROK + INDEX + IMM8 + REGREQ,    0x81,    "CMP"    },
  161.     { DIROK + INDEX + IMM8,            0x81,    "CMPA"    },
  162.     { DIROK + INDEX + IMM8,            0xc1,    "CMPB"    },
  163.     { INDEX + REGOPT,            0x43,    "COM"    },
  164.     { NULL,                    0x43,    "COMA"    },
  165.     { NULL,                    0x53,    "COMB"    },
  166.     { DIROK + INDEX + IMM16,        0x8c,    "CPX"    },
  167.     { NULL,                    0x19,    "DAA"    },
  168.     { INDEX + REGOPT,            0x4a,    "DEC"    },
  169.     { NULL,                    0x4a,    "DECA"    },
  170.     { NULL,                    0x5a,    "DECB"    },
  171.     { NULL,                    0x34,    "DES"    },
  172.     { NULL,                    0x09,    "DEX"    },
  173.     { TWO_OP + INDEX,            0x75,    "EIM"    },
  174.     { PSEUDO + ISIF,            ELSE,    "ELSE"    },
  175.     { PSEUDO,                END,    "END"    },
  176.     { PSEUDO + ISIF,            ENDI,    "ENDI"    },
  177.     { DIROK + INDEX + IMM8 + REGREQ,    0x88,    "EOR"    },
  178.     { DIROK + INDEX + IMM8,            0x88,    "EORA"    },
  179.     { DIROK + INDEX + IMM8,            0xc8,    "EORB"    },
  180.     { PSEUDO,                EQU,    "EQU"    },
  181.     { PSEUDO,                FCB,    "FCB"    },
  182.     { PSEUDO,                FCC,    "FCC"    },
  183.     { PSEUDO,                FDB,    "FDB"    },
  184.     { PSEUDO + ISIF,            IF,    "IF"    },
  185.     { INDEX + REGOPT,            0x4c,    "INC"    },
  186.     { NULL,                    0x4c,    "INCA"    },
  187.     { NULL,                    0x5c,    "INCB"    },
  188.     { PSEUDO,                INCL,    "INCL"    },
  189.     { NULL,                    0x31,    "INS"    },
  190.     { NULL,                    0x08,    "INX"    },
  191.     { INDEX,                0x4e,    "JMP"    },
  192.     { DIROK + INDEX,            0x8d,    "JSR"    },
  193.     { DIROK + INDEX + IMM8 + REGREQ,    0x86,    "LDA"    },
  194.     { DIROK + INDEX + IMM8,            0x86,    "LDAA"    },
  195.     { DIROK + INDEX + IMM8,            0xc6,    "LDAB"    },
  196.     { DIROK + INDEX + IMM16,        0xcc,    "LDD"    },
  197.     { DIROK + INDEX + IMM16,        0x8e,    "LDS"    },
  198.     { DIROK + INDEX + IMM16,        0xce,    "LDX"    },
  199.     { INDEX + REGOPT,            0x48,    "LSL"    },
  200.     { NULL,                    0x48,    "LSLA"    },
  201.     { NULL,                    0x58,    "LSLB"    },
  202.     { NULL,                    0x05,    "LSLD"    },
  203.     { INDEX + REGOPT,            0x44,    "LSR"    },
  204.     { NULL,                    0x44,    "LSRA"    },
  205.     { NULL,                    0x54,    "LSRB"    },
  206.     { NULL,                    0x04,    "LSRD"    },
  207.     { NULL,                    0x3d,    "MUL"    },
  208.     { INDEX + REGOPT,            0x40,    "NEG"    },
  209.     { NULL,                    0x40,    "NEGA"    },
  210.     { NULL,                    0x50,    "NEGB"    },
  211.     { NULL,                    0x01,    "NOP"    },
  212.     { TWO_OP + INDEX,            0x72,    "OIM"    },
  213.     { DIROK + INDEX + IMM8 + REGREQ,    0x8a,    "ORA"    },
  214.     { DIROK + INDEX + IMM8,            0x8a,    "ORAA"    },
  215.     { DIROK + INDEX + IMM8,            0xca,    "ORAB"    },
  216.     { PSEUDO,                ORG,    "ORG"    },
  217.     { PSEUDO,                PAGE,    "PAGE"    },
  218.     { REGREQ,                0x36,    "PSH"    },
  219.     { NULL,                    0x36,    "PSHA"    },
  220.     { NULL,                    0x37,    "PSHB"    },
  221.     { NULL,                    0x3c,    "PSHX"    },
  222.     { REGREQ,                0x32,    "PUL"    },
  223.     { NULL,                    0x32,    "PULA"    },
  224.     { NULL,                    0x33,    "PULB"    },
  225.     { NULL,                    0x38,    "PULX"    },
  226.     { PSEUDO,                RMB,    "RMB"    },
  227.     { INDEX + REGOPT,            0x49,    "ROL"    },
  228.     { NULL,                    0x49,    "ROLA"    },
  229.     { NULL,                    0x59,    "ROLB"    },
  230.     { INDEX + REGOPT,            0x46,    "ROR"    },
  231.     { NULL,                    0x46,    "RORA"    },
  232.     { NULL,                    0x56,    "RORB"    },
  233.     { NULL,                    0x3b,    "RTI"    },
  234.     { NULL,                    0x39,    "RTS"    },
  235.     { NULL,                    0x10,    "SBA"    },
  236.     { DIROK + INDEX + IMM8 + REGREQ,    0x82,    "SBC"    },
  237.     { DIROK + INDEX + IMM8,            0x82,    "SBCA"    },
  238.     { DIROK + INDEX + IMM8,            0xc2,    "SBCB"    },
  239.     { NULL,                    0x0d,    "SEC"    },
  240.     { NULL,                    0x0f,    "SEI"    },
  241.     { PSEUDO,                SET,    "SET"    },
  242.     { NULL,                    0x0b,    "SEV"    },
  243.     { NULL,                    0x1a,    "SLP"    },
  244.     { DIROK + INDEX + REGREQ,        0x87,    "STA"    },
  245.     { DIROK + INDEX,            0x87,    "STAA"    },
  246.     { DIROK + INDEX,            0xc7,    "STAB"    },
  247.     { DIROK + INDEX,            0xcd,    "STD"    },
  248.     { DIROK + INDEX,            0x8f,    "STS"    },
  249.     { DIROK + INDEX,            0xcf,    "STX"    },
  250.     { DIROK + INDEX + IMM8 + REGREQ,    0x80,    "SUB"    },
  251.     { DIROK + INDEX + IMM8,            0x80,    "SUBA"    },
  252.     { DIROK + INDEX + IMM8,            0xc0,    "SUBB"    },
  253.     { DIROK + INDEX + IMM16,        0x83,    "SUBD"    },
  254.     { NULL,                    0x3f,    "SWI"    },
  255.     { NULL,                    0x16,    "TAB"    },
  256.     { NULL,                    0x06,    "TAP"    },
  257.     { NULL,                    0x17,    "TBA"    },
  258.     { TWO_OP + INDEX,            0x7b,    "TIM"    },
  259.     { PSEUDO,                TITL,    "TITL"    },
  260.     { NULL,                    0x07,    "TPA"    },
  261.     { INDEX + REGOPT,            0x4d,    "TST"    },
  262.     { NULL,                    0x4d,    "TSTA"    },
  263.     { NULL,                    0x5d,    "TSTB"    },
  264.     { NULL,                    0x30,    "TSX"    },
  265.     { NULL,